package com.example.gradu.graduationproject.controller;

import com.example.gradu.graduationproject.domain.User;
import com.example.gradu.graduationproject.service.UserService;
import com.example.gradu.graduationproject.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;

/**
 * 用户表(User)表控制层
 * 关于用户的所有接口
 * @author makejava
 * @since 2025-02-12 16:19:19
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 本地存储路径
     */
    private final String uploadDir = "E:/GraduationProject/upload/";
    private final String imgAccessUrl = "http://localhost:8081/image/";
    public static class EditPwdDTO {
        private Integer userid;
        private String oldPwd;
        private String newPwd;

        public Integer getUserid() {
            return userid;
        }

        public void setUserid(Integer userid) {
            this.userid = userid;
        }

        public String getOldPwd() {
            return oldPwd;
        }

        public void setOldPwd(String oldPwd) {
            this.oldPwd = oldPwd;
        }

        public String getNewPwd() {
            return newPwd;
        }

        public void setNewPwd(String newPwd) {
            this.newPwd = newPwd;
        }
        // Getter 和 Setter
    }
    /**
     * 全查询
     *
     * @param user 筛选条件
     * @return 查询结果
     */
    @GetMapping("queryAll")
    public R queryAll(User user) {
        return this.userService.queryAll(user);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return this.userService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody User user) {
        return this.userService.insert(user);
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    public R edit(@RequestBody User user) {
        return this.userService.update(user);
    }

    /**
     * 上传头像
     *
     * @param file 用户新上传的头像文件
     * @return 是否上传成功
     */
    @PostMapping("avatar")
    public R uploadAvatar (@RequestParam("file")MultipartFile file,@RequestParam("userid")Integer userid) {
        if(file.isEmpty()){
            return R.fail().setData("文件不能为空");
        }
        try{
            File dir = new File(uploadDir);
            if(!dir.exists()){
                dir.mkdirs();
            }
            // 生成唯一文件名
            String fileName = UUID.randomUUID()+"_"+file.getOriginalFilename();
            String filePath = uploadDir + fileName;

            // 保存文件
            file.transferTo(Paths.get(filePath));

            // 更新数据库中的头像信息
            User user = new User();
            user.setUserid(userid);
            user.setUserAvater(imgAccessUrl+fileName);
            userService.update(user);

            return R.ok().setData("上传成功");
        }catch (Exception e){
            return R.ok().setData("上传失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public R deleteById(Integer id) {
        return this.userService.deleteById(id);
    }

    /**
     * 邮箱+密码登录
     *
     * @param user 邮箱密码
     * @return 是否登录成功
     */
    @PostMapping("login")
    public R loginByEmailPwd(@RequestBody User user) {
        return this.userService.loginByEmailPwd(user);
    }

    /**
     * 发验证码邮件
     *
     * @param user 邮箱
     * @return 发送情况
     */
    @PostMapping("getEmail")
    public R send(@RequestBody User user) {
        return this.userService.send(user);
    }

    /**
     * 注册
     *
     * @param user 邮箱，用户名，密码
     * @param code 验证码
     * @return 注册情况
     */
    @PutMapping("regis")
    public R RegisByEmail(@RequestBody User user,@RequestParam String code){
        return this.userService.RegisByEmail(user,code);
    }


    @PostMapping("editPwd")
    public R editPwd(@RequestBody EditPwdDTO dto){
        Integer userid = dto.getUserid();
        String oldPwd = dto.getOldPwd();
        String newPwd = dto.getNewPwd();
        // 检查旧密码是否正确
        String originalPwd = userService.queryById(userid).getData().getPassword();
        if(originalPwd.equals(newPwd)){

            return R.fail().setData("旧密码错误");
        }
        // 用新密码更新用户信息
        User user = new User();
        user.setUserid(userid);
        user.setPassword(newPwd);
        userService.update(user);

        return R.ok().setData("密码修改成功");
    }
}


